package logic

import (
	"context"
	"shell/model/mysql"

	"shell/shellrpc/internal/svc"
	"shell/shellrpc/shellrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type HomeListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHomeListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HomeListLogic {
	return &HomeListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HomeListLogic) HomeList(in *shellrpc.HomeListRequest) (*shellrpc.HomeListResponse, error) {
	// todo: add your logic here and delete this line

	var homes []mysql.Home
	mysql.DB.Debug().Find(&homes)

	var homeList []*shellrpc.HomeList
	for _, home := range homes {
		list := shellrpc.HomeList{
			Title: home.Title,
			Desc:  home.Desc,
			Price: float32(home.Price),
			Uid:   int64(home.Uid),
		}
		homeList = append(homeList, &list)
	}

	return &shellrpc.HomeListResponse{
		List: homeList,
	}, nil
}
